package com.mzpai.product.service.impl;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mzpai.product.constant.ProductConstants;
import com.mzpai.product.mapper.StoreCategoryMapper;
import com.mzpai.product.model.StoreCategory;
import com.mzpai.product.service.StoreCategoryService;
import com.mzpai.service.impl.BaseServiceImpl;
import com.mzpai.util.RedisUtil;
/**
 *
 * <b>商品分类 | Service</b><br>
 *
 * yanglijun 2021-03-16 18:45:40
 * 
 *  @version 1.0.0
 *
 */
@Service
public class StoreCategoryServiceImpl extends BaseServiceImpl<StoreCategory> implements StoreCategoryService {

	Logger logger = LoggerFactory.getLogger(StoreCategoryServiceImpl.class);
	
	@Autowired 
	private StoreCategoryMapper mapper;
	
	@Autowired
	private StringRedisTemplate redisTemplate;

	@Override
	public StoreCategory queryByCateCode(String cateCode) {
		StoreCategory storeCategory=mapper.queryByCateCode(cateCode);
		return storeCategory;
	}
	
	/**
	 * 保存
	 */
	@Override
	public int save(StoreCategory target) {
		super.save(target);
		return  target.getId();
	}

	/***
	 * 根据pid, 获取所有叶子节点数据
	 * 返回list
	 */
	@Override
	public List<StoreCategory> getStoreCategoryLastChild(Integer pid) {
		List<StoreCategory> resTemList = new LinkedList<StoreCategory>();
		List<StoreCategory> list = mapper.getList(null);
		for(StoreCategory o : list) {
			if(o.getPid().intValue() == pid.intValue()) {
				if("true".equals(o.getIsParent())) {
					addChild(list, resTemList, o.getId());
				} else {
					resTemList.add(o);
				}
			}
		}
		return resTemList;
	}

	private void addChild(List<StoreCategory> list, List<StoreCategory> resTemList, Integer pid) {
		for(StoreCategory o : list) {
			if(o.getPid().intValue() == pid.intValue()) {
				if("true".equals(o.getIsParent())) {
					addChild(list, resTemList, o.getId());
				} else {
					resTemList.add(o);
				}
			}
		}
	}

	/**
	 * 根据id，获取分类层级， 猫粮.KALU.幼猫
	 * @param id
	 * @return
	 */
	@Override
	public String getFullCategoryName(Integer id) {
		String cateNameF = "";
		StoreCategory info = mapper.queryById(id + "");
		if(info != null) {
			cateNameF = info.getCateName();
		}
		
		if(!"0".equals(info.getPid().toString())) {
			info = mapper.queryById(info.getPid() + "");
			if(info != null) {
				cateNameF = info.getCateName() + "." + cateNameF;
			}
		}
		
		if(!"0".equals(info.getPid().toString())) {
			info = mapper.queryById(info.getPid() + "");
			if(info != null) {
				cateNameF = info.getCateName() + "." + cateNameF;
			}
		}
		
		return cateNameF;
	}

	@Override
	public String getFullCategoryNameByCateCode(String cateCode) {
		String cateNameF = "";
		StoreCategory info = mapper.queryByCateCode(cateCode);
		if(info != null) {
			cateNameF = info.getCateName();
		}
		
		if(!"0".equals(info.getPid().toString())) {
			info = mapper.queryById(info.getPid() + "");
			if(info != null) {
				cateNameF = info.getCateName() + "." + cateNameF;
			}
		}
		
		if(!"0".equals(info.getPid().toString())) {
			info = mapper.queryById(info.getPid() + "");
			if(info != null) {
				cateNameF = info.getCateName() + "." + cateNameF;
			}
		}
		
		return cateNameF;
	}

	@Override
	public StoreCategory queryByPrimaryId(Integer id) {
		
		StoreCategory storeCategory=null;
		String obj=(String) new RedisUtil(redisTemplate).get(ProductConstants.SOTRE_CATEGORY_OBJ+id);
		if(StringUtils.isBlank(obj)){
			storeCategory=mapper.queryByPrimaryId(id);
			if(storeCategory!=null){
			   String jsonStr=JSON.toJSONString(storeCategory);
				//设置缓存30分钟
				new RedisUtil(redisTemplate).set(ProductConstants.SOTRE_CATEGORY_OBJ+id,jsonStr,1200);
			}
		}else{
			storeCategory = JSON.parseObject(obj,StoreCategory.class);
		}
		
		return storeCategory;
	}

	@Override
	public List<StoreCategory> queryList(Map<String, Object> map) {
		List<StoreCategory> list=mapper.queryList(map);
		return list;
	}

	/**
	 * 根据门店，查询
	 * @param storeId
	 * @return
	 */
	@Override
	public List<StoreCategory> queryByStoreId(int storeId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("storeId", storeId);
		map.put("isShow", "1");
		return mapper.queryList(map);
	}

	/***
	 * 批量更新排序
	 * @param list
	 */
	@Override
	public void updateSort(List<StoreCategory> list) {
		for(StoreCategory s : list) {
			mapper.update(s);
		}
	}
}